﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Cosmetic.DataEntity;
using System.Data;
using System.Data.SqlClient;

namespace Cosmetic.Logic
{
    public class DiscountProductManagement : LogicBase
    {

        public  IEnumerable  GetAllProduct ()
        {
            var aa = from table in m_CosmeticDC.ProductDiscounts
                     join product in m_CosmeticDC.Products
                     on table.ProductId equals product.ProductId
                     select new
                     {
                         ProductCode=product.ProductCode,
                         pname = product.Name,
                         ProductId = table.ProductId,
                         discountPrice = table.discountPrice
                     };
            return aa;
        }

        public IEnumerable GetDiscountProducts()
        {
            var aa = from table in m_CosmeticDC.ProductDiscounts
                     join product in m_CosmeticDC.Products
                     on table.ProductId equals product.ProductId
                     select new
                     {
                         ProductCode = product.ProductCode,
                         pname = product.Name,
                         ProductId = table.ProductId,
                         UID = product.UID,
                         RecommendPicture1 = product.RecommendPicture1,
                         eprice = product.eprice,
                         DiscountPrice = table.discountPrice,
                     };
            return aa;
        }


        public void InsertProduct(ProductDiscount dis)
        {
           // ProductDiscount pro = m_CosmeticDC.ProductDiscounts.Single(aa => aa.ProductId == dis.ProductId);
            var pro = from table in m_CosmeticDC.ProductDiscounts
                      where table.ProductId == dis.ProductId
                      select table;
            if (pro.Count() == 0)
            {
                m_CosmeticDC.ProductDiscounts.InsertOnSubmit(dis);
                m_CosmeticDC.SubmitChanges();
            }
        }

        public void DeleteProduct(Guid? pid)
        {
            ProductDiscount pro = m_CosmeticDC.ProductDiscounts.Single(aa => aa.ProductId == pid);
            if (pro != null)
            {
                m_CosmeticDC.ProductDiscounts.DeleteOnSubmit(pro);
                m_CosmeticDC.SubmitChanges();
            }
        }

    }//end class
}
